package com.clayecloud.module_report_repair.views;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.clayecloud.module_report_repair.R;
import com.clayecloud.module_report_repair.adapter.ReportRepairListAdapter;
import com.clayecloud.module_report_repair.databinding.ActivityRepostRepairListBinding;
import com.clayecloud.module_report_repair.entity.ReportRepair;
import com.clayecloud.module_report_repair.viewModels.ReportRepairListViewModel;
import com.clayecloud.module_report_repair.viewModels.ReportRepairMainViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/repair/list")
public class ReportRepairListActivity extends AppCompatActivity {

    private Context context = this;

    private ActivityRepostRepairListBinding activityRepostRepairListBinding;

    private ReportRepairListViewModel reportRepairListViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityRepostRepairListBinding = DataBindingUtil.setContentView(this, R.layout.activity_repost_repair_list);
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        reportRepairListViewModel = viewModelProvider.get(ReportRepairListViewModel.class);
        // 初始化函数
        init();
        // 绑定监听事件
        initListener();
    }

    /**
     * 初始化方法
     */
    private void init() {
        // 控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        reportRepairListViewModel.leadData();
        activityRepostRepairListBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    /**
     * 事件监听绑定
     */
    private void initListener() {
        activityRepostRepairListBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
                overridePendingTransition(R.anim.window_fake, R.anim.window_right_out);
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });

        reportRepairListViewModel.getReportRepairList().observe(this, new Observer<List<ReportRepair>>() {
            @Override
            public void onChanged(List<ReportRepair> reportRepairs) {
                ReportRepairListAdapter reportRepairListAdapter = new ReportRepairListAdapter(context, reportRepairs);
                activityRepostRepairListBinding.recyclerView.setAdapter(reportRepairListAdapter);
            }
        });

    }

}
